<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>生鲜商城</title>
    <!-- 引入样式 -->
    <link
            rel="stylesheet"
            href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"
    />
    <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="js/jquery.js"></script>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<el-container id="app">
    <!--头部导航 -->
    <el-header style="height:510px">
        <el-menu
                :default-active="activeIndex"
                class="el-menu-demo"
                mode="horizontal"
                @select="handleSelect"
        >
            <div class="demo-image">
                <div class="block" :key="img.headbg">
                    <el-image
                            style="width: 1920px; height: 440px"
                            :src="img.headbg"
                            :fit="img.headbg">
                    </el-image>
                </div>
            </div>
            <el-menu-item index="0">首页</el-menu-item>
            <el-menu-item index="1">用户</el-menu-item>
            <el-menu-item index="2">商品</el-menu-item>
            <el-menu-item index="3">优惠券</el-menu-item>
            <el-menu-item index="4">秒杀</el-menu-item>
            <el-menu-item index="5">数据统计</el-menu-item>
            <el-menu-item index="6">登出</el-menu-item>
        </el-menu>
        <div class="line"></div>
    </el-header>
    <!-- 主体部分 请在下方写代码 -->
    <el-main>
        <el-button type="success"  size="mini" @click="open" plain>新增秒杀</el-button>

        <el-dialog
                title="添加秒杀商品"
                :visible.sync="dialogVisible"
                width="30%"
                :before-close="handleClose">

            <el-form ref="form" v-model="form" label-width="80px">
                <el-form-item label="商品类别">
                    <el-select v-model="product.category"  placeholder="请选择商品类别" v-on:change="change($event)">
                        <el-option v-for="productCategory in form.productCategorys" v-bind:label="productCategory.productTypeName"  v-bind:value="productCategory.productTypeId" ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商品名字">
                    <el-select v-model="product.name"  placeholder="请选择商品名字" >
                        <el-option v-for="productName in form.productNames" v-bind:label="productName.productName"  v-bind:value="productName.productId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="活动时间">
                    <el-col :span="11">
                        <el-date-picker type="date" value-format="yyyy-MM-dd" placeholder="选择日期"  v-model="product.date1" style="width: 100%;"></el-date-picker>
                    </el-col>
                    <el-col class="line" :span="2">-</el-col>
                    <el-col :span="11">
                        <el-time-picker placeholder="选择时间" value-format="HH:mm:ss" v-model="product.date2" style="width: 100%;"></el-time-picker>
                    </el-col>
                </el-form-item>

                <el-form-item label="持续时间">
                    <el-select v-model="product.time"  placeholder="请选择时间" >
                        <el-option label="半小时" value="0.5"></el-option>
                        <el-option label="1小时" value="1"></el-option>
                        <el-option label="2小时" value="2"></el-option>
                        <el-option label="一天" value="24"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="商品数量">
                    <el-input type="number" min="1" :plain="true" placeholder="请输入内容" v-model="product.number" @blur="checkNum(product.number)" clearable>
                    </el-input>
                </el-form-item>
                <el-form-item label="商品折扣">
                    <el-select v-model="product.discount"  placeholder="请选择折扣" >
                        <el-option label="98折" value="0.98"></el-option>
                        <el-option label="95折" value="0.95"></el-option>
                        <el-option label="9折" value="0.9"></el-option>
                        <el-option label="88折" value="0.88"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="success" @click="onSubmit">立即添加</el-button>
                    <el-button type="info" @click="close">取消</el-button>
                </el-form-item>
            </el-form>
        </el-dialog>


        <el-table
                :data="KillProduct"
                border
                height="250"
                style="width: 100%">
            <el-table-column
                    prop="productName"
                    label="商品名字"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="productPrice"
                    label="商品原价"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="limitedTimeOfferDiscount"
                    label="商品折扣">
            </el-table-column>
            <el-table-column
                    prop="limitedTimeOfferDuration"
                    label="商品活动持续时间">
            </el-table-column>
            <el-table-column
                    prop="limitedTimeOfferSale"
                    label="商品售卖数量">
            </el-table-column>
            <el-table-column
                    prop="limitedTimeOfferTotalNum"
                    label="商品总数量">
            </el-table-column>
            <el-table-column
                    prop="limitedTimeOfferStartTime"
                    label="商品开始时间">
            </el-table-column>
        </el-table>

    </el-main>
</el-container>


<script>
    var app = new Vue({
        el: "#app",
        data: {
            //此处请改为对应的选项
            activeIndex: "4",
            img: {
                headbg: '/img/bg/banner3back.jpg',
            },
            dialogVisible: false,
            form:{
                productCategorys:[],
                productNames:[],
                date1: '',
                date2: '',
                time:'',
                number: '',
                discount:''
            },
            product:{
                category:'',
                name:'',
                date1: '',
                date2: '',
                time:'',
                number: '',
                discount:''
            },
            KillProduct:[],
            tableData: [{
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-04',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1517 弄'
            }, {
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
            }, {
                date: '2016-05-03',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1516 弄'
            }]
        },
        methods: {
            //导航 添加中
            handleSelect: function (key, keyPath) {
                console.log(key, keyPath);
                if(key==0){
                    window.location.href="/index"
                }else if(key==1){
                    window.location.href="/user"
                }else if(key==2){
                    window.location.href="/product"
                }else if(key==3){
                    window.location.href="/coupon"
                }else if(key==4){
                    window.location.href="/limited"
                }else if(key==5){
                    window.location.href="/statistics"
                }else if(key==6){
                    $.ajax({
                        url:"/logout",
                    })
                }
            },

            onSubmit() {
                let p=app.product;
                let date=p.date1+" "+p.date2;
                let registerProduct={productId:p.name,date:date,time:p.time,number:p.number,discount:p.discount,productName:p.category}
                axios.post("/freshmarket/limited-time-offer/registerKillProduct",registerProduct)


                    .then(function (resp) {


                        if(resp.data.data=="添加成功"){
                            app.dialogVisible=false;
                            app.product.category='';
                            app.product. name='';
                            app.product. date1='';
                            app.product.date2= '';
                            app.product.time='';
                            app.product.number='';
                            app.product.discount='';
                            app.$message({
                                message: resp.data.data,
                                type: 'success'
                            });
                            return ;
                        }
                        app.$message({
                            message: "添加失败"+resp.data.data,
                            type: 'warning'
                        });

                    })
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        app.product.category='';
                        app.product. name='';
                        app.product. date1='';
                        app.product.date2= '';
                        app.product.time='';
                        app.product.number='';
                        app.product.discount='';
                        app.form.productNames=[];
                        done();
                    })
                    .catch(_ => {});
            }	,
            change(e){

                axios.get("/freshmarket/limited-time-offer/findByProductTypeId",{
                    params:{id:e}
                })
                    .then(function (resp) {
                        console.log(resp.data.data[0].productName);
                        app.form.productNames=resp.data.data
                        app.product.name='';
                    })
            },


            checkNum(e){
                axios.get("/freshmarket/limited-time-offer/numbers",{
                    params:{id:app.product.name==''?0:app.product.name,numbers:e==''?0:e}
                })
                    .then(function (resp) {
                        if(resp.data.data!=null){
                            app.$message({
                                message: resp.data.data,
                                type: 'warning'
                            });
                        }
                    })
            },
            open(){
                app.dialogVisible = true;
                axios.get("/freshmarket/limited-time-offer/findNames")
                    .then(function (resp) {
                        app.form.productCategorys=resp.data.data
                    })
            },
            close(){
                app.dialogVisible = false;

                app.product.category='';
                app.product. name='';
                app.product. date1='';
                app.product.date2= '';
                app.product.time='';
                app.product.number='';
                app.product.discount='';
                app.form.productNames=[];
            }

        },
        //生命周期初始化
        created(){
            axios.get("/freshmarket/limited-time-offer/findAll")
                .then(function (resp) {
                    app.KillProduct=resp.data.data
                })
        }
    });
</script>
</body>
</html>
